package cn.tedu.javagame.ball;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class BallPanel extends JPanel implements MouseMotionListener {
    Color bgColor = new Color(215, 219, 251);
    Ball ball = new Ball();//球对象
    Board board =  new Board();//当板对象
    private List<Brick> bricks; // 存储所有方块的列表
    int tickCount = 0; // 记录定时器执行的次数
    final int INCREASE_INTERVAL = 1000; // 每500次定时器执行增加一次速度
    // 方块的属性
    private static final int BLOCK_ROWS = 3;
    private static final int BLOCK_COLS = 6;
    private static final int BLOCK_MARGIN = 20; // 方块整体与边界的间距
    private static final int BLOCK_SPACING = 20; // 方块之间的间距
    private Color[][] blockColors; // 存储每个方块的颜色

    public BallPanel(){
        this.setBackground(bgColor);
        action();
        this.addMouseMotionListener(this);
        initializeBricks(); // 初始化方块颜色
    }

    public void initializeBricks() {
        bricks = new ArrayList<>();
        int blockWidth = (BallFrame.WIDTH - 2 * BLOCK_MARGIN - (BLOCK_COLS - 1) * BLOCK_SPACING) / BLOCK_COLS;
        int blockHeight = (BallFrame.HEIGHT / 3 - 2 * BLOCK_MARGIN - (BLOCK_ROWS - 1) * BLOCK_SPACING) / BLOCK_ROWS;
        for (int i = 0; i < BLOCK_ROWS; i++) {
            for (int j = 0; j < BLOCK_COLS; j++) {
                int x = BLOCK_MARGIN + j * (blockWidth + BLOCK_SPACING);
                int y = BLOCK_MARGIN + i * (blockHeight + BLOCK_SPACING);
                Color color = getRandomColor();
                bricks.add(new Brick(x, y, blockWidth, blockHeight, color));
            }
        }
    }

    // 获取随机颜色
    public Color getRandomColor() {
        Color[] colors = {
                Color.RED,
                Color.YELLOW,
                Color.BLUE,
                Color.GREEN,
                new Color(128, 0, 128) // 紫色
        };
        return colors[(int) (Math.random() * colors.length)];
    }
    public void paint(Graphics g){
        super.paint(g);
        g.setColor(ball.color);
        g.fillOval(ball.x, ball.y, ball.length, ball.length);

        g.setColor(Color.BLACK);
        g.fillRect(board.x, board.y, board.width, board.height);

        // 在左上角显示球速
        g.setColor(Color.WHITE);
        g.setFont(new Font("Arial", Font.BOLD, 16));
        g.drawString("球速: " + ball.getSpeed(), 10, 20);

        // 计算方块的宽度和高度
        int blockWidth = (BallFrame.WIDTH - 2 * BLOCK_MARGIN - (BLOCK_COLS - 1) * BLOCK_SPACING) / BLOCK_COLS;
        int blockHeight = (BallFrame.HEIGHT / 3 - 2 * BLOCK_MARGIN - (BLOCK_ROWS - 1) * BLOCK_SPACING) / BLOCK_ROWS;

        // 绘制方块，只绘制可见的方块
        for (Brick brick : bricks) {
            if (brick.isVisible()) {
                g.setColor(brick.color);
                g.fillRect(brick.x, brick.y, brick.width, brick.height);
            }
        }
    }

    public void action(){
        java.util.Timer timer = new Timer();
        timer.schedule(new TimerTask(){
            public void run(){
                ball.move();
                board.catchBall(ball);
                // 处理球与方块的碰撞
                for (Brick brick : bricks) {
                    if (brick.isVisible()) {
                        if (ball.hitBrick(brick)) {
                            brick.handleCollision(ball);
                        }
                    }
                }
                tickCount++;
                // 每 INCREASE_INTERVAL 次增加一次球的速度
                if (tickCount % INCREASE_INTERVAL == 0) {
                    ball.increaseSpeed();
                }
                //重绘，刷新的意思
                repaint();
            }
        },10,20);
    }


    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        int newX = e.getX() - board.width / 2; // 使鼠标位于板的中间
        // 边界检查，确保板的右边不超过窗口右边界
        if (newX + board.width > BallFrame.WIDTH) {
            newX = BallFrame.WIDTH - board.width;
        }
        // 确保板的左边不超过窗口左边界
        if (newX < 0) {
            newX = 0;
        }
        board.move(newX);
    }
}
